

import requests
import re

conTextAll="a"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}

response = requests.get('###url', headers=headers)



def download_img(img_url):
    print (img_url)
    img_nick=str(img_url.replace('/','_').replace(':','_'))
    print('data/img/'+ img_nick)
    r = requests.get(img_url, headers=headers, stream=True)
    print(r.status_code) # 返回状态码
    if r.status_code == 200:
        open('data/img/'+ img_nick, 'wb').write(r.content) # 将内容写入图片
        print("done")
    del r


# 获取状态
#print(response.status_code)
#print(response.content.decode())
pagetext=response.content.decode()
links=re.findall(r'(?<=<link href=\").*?(?=\")|(?<=href=\').*?(?=\')|(?<=src=\').*?(?=\')', pagetext)
print(links)
for i in links:
    i=i.replace('./','###url')
    #print(i)
    response2 = requests.get(i, headers=headers)
    #print(response2.status_code)
    #print(response2.content.decode())
    conTextAll=conTextAll+response2.content.decode()


#print(conTextAll)
PATTERN = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
linksAll=re.findall(PATTERN, conTextAll)
picsAll=re.findall(r'"img/.*?"|img/.*?\)', conTextAll)
print(linksAll)
print(picsAll)

fo=open("data/Links.txt","w")
fo.write(str(linksAll))
fo.close
for img in picsAll:
    img_url = "###url"+str(img.replace('"',''))
    download_img(img_url)